﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.Web.Filters;
using ASPItalia.ModelVirtualCasting.Web.Models;

namespace ASPItalia.ModelVirtualCasting.Web.Controllers
{
    public class ModelController : Controller
    {
        private IModelRepository repository;

        internal ModelController(IModelRepository repository)
        {
            this.repository = repository;
        }

        public ModelController() :
            this(ApplicationContext.Current.Models)
        { }

        #region Search
        [HttpGet]
        [ProvideCountries]
        public ActionResult Search()
        {
            return View(new SearchModel());
        }

        [HttpPost]
        public ActionResult Search(SearchModel model)
        {
            // in questo modo diamo all'utente la possibilità
            // di salvare la ricerca tra i bookmark
            string searchFilter = model.Serialize();

            return this.RedirectToAction("Results", new { filter = searchFilter });
        }

        public ActionResult Results(string filter, int? pageNumber)
        {
            if (pageNumber == null)
                pageNumber = 1;

            /* si può usare anche dentro un blocco try..catch per 
             * gestire dati di input non validi */
            var searchModel = SearchModel.Deserialize(filter);

            var query = repository.Include("ResidenceCountry").GetAll();

            if (!string.IsNullOrEmpty(searchModel.Nickname))
            {
                query = query.Where(c => c.NickName.StartsWith(searchModel.Nickname));
            }

            if (!string.IsNullOrEmpty(searchModel.Firstname))
            {
                query = query.Where(c => c.FirstName.StartsWith(searchModel.Firstname));
            }

            if (!string.IsNullOrEmpty(searchModel.Gender))
            {
                query = query.Where(c => c.Gender == searchModel.Gender);
            }

            if (searchModel.CurrentCountry.CountryID > 0)
            {
                query = query.Where(c => c.ResidenceCountry.CountryID == searchModel.CurrentCountry.CountryID);
            }

            // e così via...
            int totalPages = (int)Math.Ceiling(Convert.ToDecimal(query.Count()) / pageSize);
            query = query
                .OrderBy(c => c.NickName)
                .Skip((pageNumber.Value - 1) * pageSize)
                .Take(pageSize);

            SearchModelResult model = new SearchModelResult(pageNumber.Value, totalPages, query);
            
            if (this.HttpContext != null)
                model.BaseUrl = Url.Action("Results", new { filter = filter, pageNumber = "[page]" });

            return this.View(model);
        }

        private const int pageSize = 3;
        #endregion
    }
}